package com.android.duininsuoai.view.group;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.duininsuoai.R;

import java.util.Timer;
import java.util.TimerTask;

@SuppressLint("HandlerLeak")
public class CountDownTimerView extends LinearLayout {

//    private TextView tv_hour_decade;
//    private TextView tv_hour_unit;
//    private TextView tv_min_decade;
//    private TextView tv_min_unit;
//    private TextView tv_sec_decade;
//    private TextView tv_sec_unit;

    private Context context;
    private int days_decade;
    private int hour_decade;
    private int hour_unit;
    private int min_decade;
    private int min_unit;
    private int sec_decade;
    private int sec_unit;

    private Timer timer;

    private Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            countDown();
        }
    };
    private final TextView day_tv;
    private final TextView hour_tv_decade;
    private final TextView hour_tv_unit;
    private final TextView min_tv_decade;
    private final TextView min_tv_unit;
    private final TextView sec_tv_decade;
    private final TextView sec_tv_unit;
    private final View day_ll;
    private final TextView day_colon;

    public CountDownTimerView(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.view_countdowntimer, this);

        day_ll = view.findViewById(R.id.day_ll);
        day_tv = (TextView) view.findViewById(R.id.day_tv);


        View hour_ll = view.findViewById(R.id.hour_ll);
        hour_tv_decade = (TextView) view.findViewById(R.id.hour_tv_decade);
        hour_tv_unit = (TextView) view.findViewById(R.id.hour_tv_unit);


        View min_ll = view.findViewById(R.id.min_ll);
        min_tv_decade = (TextView) view.findViewById(R.id.min_tv_decade);
        min_tv_unit = (TextView) view.findViewById(R.id.min_tv_unit);


        View sec_ll = view.findViewById(R.id.sec_ll);
        sec_tv_decade = (TextView) view.findViewById(R.id.sec_tv_decade);
        sec_tv_unit = (TextView) view.findViewById(R.id.sec_tv_unit);


        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SnapUpCountDownTimerView);
        int size = array.getInteger(R.styleable.SnapUpCountDownTimerView_viewSize, 12);
        int color = array.getColor(R.styleable.SnapUpCountDownTimerView_viewColor, Color.parseColor("#333333"));
        int bg = array.getResourceId(R.styleable.SnapUpCountDownTimerView_viewBg, R.color.white);

        day_tv.setTextSize(size);
        day_tv.setTextColor(color);
        day_ll.setBackgroundResource(bg);

        hour_tv_decade.setTextSize(size);
        hour_tv_decade.setTextColor(color);
        hour_tv_unit.setTextSize(size);
        hour_tv_unit.setTextColor(color);
        hour_ll.setBackgroundResource(bg);

        min_tv_decade.setTextSize(size);
        min_tv_decade.setTextColor(color);
        min_tv_unit.setTextSize(size);
        min_tv_unit.setTextColor(color);
        min_ll.setBackgroundResource(bg);

        sec_tv_decade.setTextSize(size);
        sec_tv_decade.setTextColor(color);
        sec_tv_unit.setTextSize(size);
        sec_tv_unit.setTextColor(color);
        sec_ll.setBackgroundResource(bg);

        ((TextView) view.findViewById(R.id.hour_colon)).setTextSize(size);
        ((TextView) view.findViewById(R.id.min_colon)).setTextSize(size);
        day_colon = (TextView) view.findViewById(R.id.day_colon);
        day_colon.setTextSize(size - 3);


    }


    public void start() {
        if (timer == null) {
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    handler.sendEmptyMessage(0);
                }
            }, 0, 1000);
        }
    }


    public void stop() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }


    public void setTime(int count) {
        int day = formatDuring(count, 0);
        int hour = formatDuring(count, 1);
        int min = formatDuring(count, 2);
        int sec = formatDuring(count, 3);


        if (hour > 24 || min > 60 || sec > 60 || hour < 0 || min < 0
                || sec < 0) {

            throw new RuntimeException("时间格式错误,请检查你的代码");
        }
        days_decade = day;


        hour_decade = hour / 10;
        hour_unit = hour - hour_decade * 10;

        min_decade = min / 10;
        min_unit = min - min_decade * 10;

        sec_decade = sec / 10;
        sec_unit = sec - sec_decade * 10;


        if (day <= 0) {
            day_ll.setVisibility(GONE);
            day_colon.setVisibility(GONE);
        } else if (day < 10 && day > 0) {

            day_tv.setText("0" + days_decade + "");
        } else {

            day_tv.setText(days_decade + "");
        }
        hour_tv_decade.setText(hour_decade + "");
        hour_tv_unit.setText(hour_unit + "");
        min_tv_decade.setText(min_decade + "");
        min_tv_unit.setText(min_unit + "");
        sec_tv_decade.setText(sec_decade + "");
        sec_tv_unit.setText(sec_unit + "");
    }


    private void countDown() {
        if (isCarry4Decade(sec_tv_unit, 9)) {
            if (isCarry4Decade(sec_tv_decade, 5)) {
                if (isCarry4Decade(min_tv_unit, 9)) {
                    if (isCarry4Decade(min_tv_decade, 5)) {
                        if (isCarry4Decade(hour_tv_unit, 3)) {
                            if (isCarry4Decade(hour_tv_decade, 2)) {
                                if (isCarry4Decade(day_tv, 0)) {
                                    stop();
                                    setTime(0);//重置为0
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    private boolean isCarry4Decade(TextView tv, int limit) {

        int time = Integer.valueOf(tv.getText().toString());
        time = time - 1;
        if (time < 0) {
            time = limit;
            tv.setText(time + "");
            return true;
        } else {
            tv.setText(time + "");
            return false;
        }
    }


    public int formatDuring(long mss, int type) {
        long days = mss / (60 * 60 * 24);
        long hours = (mss % (60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % (60 * 60)) / (60);
        long seconds = (mss % (60)) / 1;

        switch (type) {
            case 0:
                return (int) days;
            case 1:
                return (int) hours;
            case 2:
                return (int) minutes;
            case 3:
                return (int) seconds;

        }
        return 0;
    }
}